package org.zgy.stock.threads.holder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.val;
import org.zgy.stock.dto.StockHolderDateDto;
import org.zgy.stock.model.holder.StockFreeHoldersInfo;
import org.zgy.stock.service.holder.StockFreeHoldersInfoService;
import org.zgy.stock.threads.DfcfResponse;
import org.zgy.stock.utils.okhttp3.OkHttpUtil;


public class StockFreeHoldersInfoThread implements Runnable {
    private static final String API = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_EH_FREEHOLDERS&columns=SECUCODE%2CSECURITY_CODE%2CEND_DATE%2CHOLDER_RANK%2CHOLDER_NEW%2CHOLDER_NAME%2CHOLDER_TYPE%2CSHARES_TYPE%2CHOLD_NUM%2CFREE_HOLDNUM_RATIO%2CHOLD_NUM_CHANGE%2CCHANGE_RATIO&quoteColumns=&filter=(SECUCODE%3D%22STOCK_CODE_WITH_MARKET%22)(END_DATE%3D%27END_DATE_WITH_MARKET%27)&pageNumber=1&pageSize=&sortTypes=1&sortColumns=HOLDER_RANK&source=HSF10&client=PC&v=05773291693462265";
    private static final String DATE_API = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_EH_FREEHOLDERSDATE&columns=SECUCODE%2CEND_DATE&quoteColumns=&filter=(SECUCODE%3D%22STOCK_CODE_WITH_MARKET%22)&pageNumber=1&pageSize=&sortTypes=-1&sortColumns=END_DATE&source=HSF10&client=PC&v=08145883851913183";
    private final String code;
    private final StockFreeHoldersInfoService stockFreeHoldersInfoService;

    public StockFreeHoldersInfoThread(String code, StockFreeHoldersInfoService stockFreeHoldersInfoService) {
        this.code = code;
        this.stockFreeHoldersInfoService = stockFreeHoldersInfoService;
    }

    @Override
    public void run() {
        String stock_code = "";
        if (code.startsWith("6")) {
            stock_code = code + ".SH";
        } else {
            stock_code = code + ".SZ";
        }
        String result = OkHttpUtil.getRequest(DATE_API.replace("STOCK_CODE_WITH_MARKET", stock_code), null);
        DfcfResponse<StockHolderDateDto> dateResponse =  JSON.parseObject(
                result,
                new TypeReference<DfcfResponse<StockHolderDateDto>>() {
                }
        );
        val dateList = dateResponse.getResult().getData();

        String finalStock_code = stock_code;
        dateList.parallelStream().forEach(endDate -> {
            try {
                String date = endDate.getEndDate().split(" ")[0];
                String api = API.replace("STOCK_CODE_WITH_MARKET", finalStock_code)
                        .replace("END_DATE_WITH_MARKET", date);
                String response = OkHttpUtil.getRequest(api, null);
                DfcfResponse<StockFreeHoldersInfo> holderResponse = JSON.parseObject(
                        response,
                        new TypeReference<DfcfResponse<StockFreeHoldersInfo>>() {
                        }
                );
                if (holderResponse.isSuccess()) {
                    stockFreeHoldersInfoService.insert(holderResponse.getResult().getData());
                } else {
                    System.err.println("请求失败: " + holderResponse.getMessage());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
